package com.bmadmin.controller.sms;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aisino.sms.client.SmsClient;
import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.MessageDomain;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.service.DelaySendService;
import com.bmadmin.service.SingleSendService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.bmadmin.util.Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/delay")
public class DelaySendController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());
	@Autowired
	DelaySendService delaySendService;
	@Autowired
	SingleSendService singleSendService;
    /**
     * 获取定时发送列表
     */
    @GetMapping("/getDelayMessage")
    public Object getDelayMessage(HttpSession session,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	MessageDomain message = new MessageDomain();
    	message.setS_create_id(user.getU_id());
//    	LayUiDataTablePOJO result = delaySendService.getDelayMessage(message);
    	PageHelper.startPage(page, limit);
		List<MessageDomain> list = delaySendService.getDelayMessageList(message);
		PageInfo pagelist = new PageInfo(list);
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());
        return result;
    }
    
    /**
     * 定时发送列表删除
     */
    @PostMapping("/deleteDelayMessage")
    public NormalResult deleteDelayMessage(MessageDomain message) {
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(message.getS_id())){
            result.setCode("9999");
            result.setMsg("定时发送传入主键ID为空!");
            return result;
        }

        result = delaySendService.deleteDelayMessage(message.getS_id());
        return result;
    }
    
    /**
     * 批量短信发送
     */
    @PostMapping("/sendDelayMessage")
    public NormalResult sendDelayMessage(HttpSession session,String s_id) {
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
    	MessageDomain message = new MessageDomain();
    	message.setS_id(s_id);
    	message.setS_create_id(user.getU_id());
    	List<MessageDomain> list = delaySendService.getDelayMessageList(message);
    	if(list==null||list.size()==0){
    		result.setCode("9999");
            result.setMsg("未找到对应的定时短信记录!");
            return result;
    	}else{
    		MessageDomain info = list.get(0);
    		
    		SmsClient smsClient = new SmsClient();
    		String phones =info.getS_phone();//要发送短信的号码数组
    		String nr = info.getS_message();//要发送的消息
    		String id = "1330212";//接入用户ID
    		String password = "LFGA6B02";//接入用户密码
    		String sn = Util.getSn();//序列号
    		String sender = "宁波市杭州湾新区国家税务局";//发送人，预留
    		String priority = "2";//优先级
    		String retResult = smsClient.sendMessageMToO(id, password, sn, phones, nr, sender,priority);
    		if(retResult.equals("0")){
    			logger.info("调用市局短信发送接口返回成功，手机号码【"+phones+"】");
    			info.setS_id(null);
        		info.setS_state("Y");
        		info.setS_type("2");
        		info.setS_create_time(BMUtil.getNowTimeForString());
        		info.setS_sendtime(BMUtil.getNowTimeForString());
            	//发送成功后，短信保存至短信日志表中
            	singleSendService.InsertMessageLog(info);
            	//删除预发送列表对应的数据
            	delaySendService.deleteDelayMessage(s_id);
            	result.setCode("0000");
                result.setMsg("短信发送成功!");
    		}else{
    			logger.info("调用市局短信发送接口返回失败，手机号码【"+phones+"】，短信内容【"+nr+"】，返回信息【"+retResult+"】");
    			delaySendService.UpdateDelayBz(s_id,"市局短信发送接口返回发送失败，返回信息【"+retResult+"】");
    			result.setCode("9999");
                result.setMsg("市局接口返回发送失败！");
    		}
    		
            return result;
    	}
    }
}
